package com.seari.custody.service.Impl;

import com.seari.custody.bean.AgendaFile;
import com.seari.custody.bean.PersonalAgenda;
import com.seari.custody.mapper.PersonalAgendaMapper;
import com.seari.custody.service.PersonalAgendaService;
import com.seari.custody.util.HandlerNullUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("PersonalAgendaService")
public class PersonalAgendaServiceImpl implements PersonalAgendaService {

    @Value("${minio.url}")
    private String minioUrl;//minio
    @Value("${minio.personagenda.bucket}")
    private String minioPersonAgenda;//minio  个人日程附件桶地址

    @Autowired
    private PersonalAgendaMapper personalAgendaMapper;

    @Override
    public List<PersonalAgenda> getAgendaByMpid(String userId, String startTime, String endTime) throws Exception {

        return personalAgendaMapper.getAgendaByMpid(userId, startTime, endTime);
    }

    @Override
    public void addAgendaInfo(PersonalAgenda personalAgenda) throws Exception {

        String uuid = personalAgenda.getUuid();
        PersonalAgenda personalAgendaOld = personalAgendaMapper.selectAgendaInfoByUuid(uuid);

        if (HandlerNullUtils.objectIsNull(personalAgendaOld)) {
            personalAgenda.setUuid(uuid);
            personalAgendaMapper.addAgendaInfo(personalAgenda);
        } else {
            personalAgendaMapper.updateAgendaInfoByUuid(personalAgenda);
        }
    }

    @Override
    public PersonalAgenda selectAgendaInfoByUuid(String uuid) throws Exception {

        return personalAgendaMapper.selectAgendaInfoByUuid(uuid);
    }

    @Override
    public List<AgendaFile> selectAgendaAttachFileList(String uuid) throws Exception {

        List<AgendaFile> agendaFilesList = personalAgendaMapper.selectAgendaAttachFileList(uuid);
        for (int i = 0; i < agendaFilesList.size(); i++) {
            agendaFilesList.get(i).setMinioFileName(minioUrl + "/personagenda/" + agendaFilesList.get(i).getMinioFileName());
            agendaFilesList.get(i).setFileStatus("上传成功");
        }
        return agendaFilesList;
    }

    @Override
    public AgendaFile selectAgendaAttachFileByFileUuid(String uuid) throws Exception {

        return personalAgendaMapper.selectAgendaAttachFileByFileUuid(uuid);
    }

    @Override
    public List<PersonalAgenda> selectAgendaInfoListByYearAndWeek(int year, int week, String userName) throws Exception {
        //下一周
        return personalAgendaMapper.selectAgendaInfoListByYearAndWeek(year, week, userName);
    }

    @Override
    public void addAgendaAttachFileInfo(AgendaFile agendaFile) throws Exception {

        personalAgendaMapper.addAgendaAttachFileInfo(agendaFile);
    }

    /**
     * 通过个人日程ID删除个人日程信息
     *
     * @param uuid
     * @throws Exception
     */
    @Override
    public void deleteAgendaInfoByUuid(String uuid) throws Exception {
        //删除个人日程信息
        personalAgendaMapper.deleteAgendaInfoByUuid(uuid);
        //删除个人日程附件信息  通过个人日程ID删除
        personalAgendaMapper.deleteAttachFileByAgendaUuid(uuid);
    }

    /**
     * 通过uuid删除附件信息
     *
     * @param uuid
     * @throws Exception
     */
    public void deleteAgendaAttachFileByFileUuid(String uuid) throws Exception {

        personalAgendaMapper.deleteAgendaAttachFileByFileUuid(uuid);
    }

}